/**
 * \license
 * Copyright Lassi Marttala 2013
 * 
 * This file is part of dlt2jpa.
 *
 * dlt2jpa is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dlt2jpa is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with dlt2jpa.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.fizzl.dlt.message;

import java.io.Serializable;


public class DltPayloadArgumentTypeInfo implements Serializable {
	public static DltPayloadArgumentTypeInfo fromInt(int typefield) {
		DltPayloadArgumentTypeInfo ret = new DltPayloadArgumentTypeInfo();
		
		int typeLenOrder = typefield & bmTypeLength;
		switch(typeLenOrder) {
		case 1:
			ret.typeLength = 1;
			break;
		case 2:
			ret.typeLength = 2;
			break;
		case 3:
			ret.typeLength = 4;
			break;
		case 4:
			ret.typeLength = 8;
			break;
		case 5:
			ret.typeLength = 16;
			break;
		default:
			ret.typeLength = 0;
			break;
		}
		ret.stringEncoding	= typefield & bmTypeStringEncoding;
		
		if(0 != (typefield & bmTypeBoolean)) 				ret.type = Type.TypeBoolean;
		else if(0 != (typefield & bmTypeSignedInteger)) 	ret.type = Type.TypeSignedInteger;
		else if(0 != (typefield & bmTypeUnsignedInteger))	ret.type = Type.TypeUnsignedInteger;
		else if(0 != (typefield & bmTypeFloat)) 			ret.type = Type.TypeFloat;
		else if(0 != (typefield & bmTypeString)) 			ret.type = Type.TypeString;
		else if(0 != (typefield & bmTypeRawData)) 			ret.type = Type.TypeRawData;
		else if(0 != (typefield & bmTypeTraceInfo)) 		ret.type = Type.TypeTraceInfo;
		else ret.type = Type.TypeUnknown;
		
		return ret;
	}
	
	public enum Type {
		TypeUnknown,
		TypeBoolean,
		TypeSignedInteger,
		TypeUnsignedInteger,
		TypeFloat,
		TypeString,
		TypeRawData,
		TypeFixedPoint,
		TypeTraceInfo;
	};

	/* Bitmasks for parsing the binary type info */
	static final int bmTypeLength			= 0b00000000000000000000000000001111;
	static final int bmTypeBoolean			= 0b00000000000000000000000000010000;
	static final int bmTypeSignedInteger	= 0b00000000000000000000000000100000;
	static final int bmTypeUnsignedInteger	= 0b00000000000000000000000001000000;
	static final int bmTypeFloat			= 0b00000000000000000000000010000000;
	static final int bmTypeArray			= 0b00000000000000000000000100000000;
	static final int bmTypeString			= 0b00000000000000000000001000000000;
	static final int bmTypeRawData			= 0b00000000000000000000010000000000;
	static final int bmTypeVariable			= 0b00000000000000000000100000000000;
	static final int bmTypeFixedPoint		= 0b00000000000000000001000000000000;
	static final int bmTypeTraceInfo		= 0b00000000000000000010000000000000;
	static final int bmTypeStruct			= 0b00000000000000000100000000000000;
	static final int bmTypeStringEncoding	= 0b00000000000000111000000000000000;
	static final int bmTypeUnused			= 0b11111111111111000000000000000000;
	
	/* Private fields */
	private Type type;
	private int typeLength;
	private int stringEncoding;
	
	
	public Type getType() {
		return type;
	}
	public void setType(Type type) {
		this.type = type;
	}
	public int getTypeLength() {
		return typeLength;
	}
	public void setTypeLength(int typeLength) {
		this.typeLength = typeLength;
	}
	public int getStringEncoding() {
		return stringEncoding;
	}
	public void setStringEncoding(int stringEncoding) {
		this.stringEncoding = stringEncoding;
	}
	
	// For Serializable
	private static final long serialVersionUID = 1L;
}
